package com.example.demo.Controller;

import com.example.demo.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/test")
public class SampleController {
    @RequestMapping("/")
    public String home(){
        return "hello Spring Boot";
    }
    @RequestMapping(value = "/getName/{name}")
    public String getName(@PathVariable("name") String name){
        return "this name is "+name;
    }
    @RequestMapping(value = "/getUser",method = RequestMethod.GET)
    public User getUser(){
        User user = new User();
        user.setBirthday(new Date());
        user.setDesc(null);
        user.setName("test");
        user.setUserId(12345);
        return user;
    }
    /*@RequestMapping(value = "/**")
    public String test1(){
        return "'**'表示匹配任意路径";
    }
    @RequestMapping(value="/*.json")
    public String test2(){
        return "'*'表示匹配任意字符串";
    }
    @RequestMapping(value = "/?.json")
    public String test3(){
        return "'？'表示匹配单个字符";
    }*/
    //consumes属性意味着HTTP头测Content-Type媒体类型与consumes相同，“application/json”接受一个AJAX请求，浏览器发的请求没有Content-Type，所以不支持
    @GetMapping(value = "/consumes/test.json",consumes = "application/json")
    public String test4(){
        return "用AJAX请求测试consumes属性";
    }
    //使用HTTP参数转JavaBean对象，/test/update.json?userId=123&name=hhh
    @GetMapping(value = "/update.json")
    public String test5(User user){
        System.out.println(user.toString());
        return "success";
    }
    //用@RequestBody接收客户端发送的JSON请求
    @PostMapping(value = "/saveUserByJson")
    public String test6(@RequestBody User user){
        return user.toString();
    }
}
